# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView
from Call_Ticket.models import Call_Ticket
from Call_Ticket.packaging import callTicket_push_boss, callTicket_push_crm
from utils.redis_operation import redis_delete, redis_query
from utils.turn_time import turn_time


# 话单回调接口
class Call_Ticket_View(APIView):
    def get(self,request):
        print('GET')
        print(request)
        return Response({"code":200,"msg":"succend"})
    def post(self,request):
        print('话单',request.data)
        appSid = request.data.get('appSid','')
        conversationSid = request.data.get('conversationSid','')
        callType = request.data.get('callType','')
        userNumber = request.data.get('userNumber','')
        userArea = request.data.get('userArea','')
        agentNo = request.data.get('agentNo','')
        agentNumber = request.data.get('agentNumber','')
        userCli = request.data.get('userCli','')
        agentCli = request.data.get('agentCli','')
        accessNumber = request.data.get('accessNumber','')
        queueId = request.data.get('queueId','')
        queueTime = turn_time(request.data.get('queueTime',''))
        startTime = turn_time(request.data.get('startTime',''))
        answerTime = turn_time(request.data.get('answerTime',''))
        ringTime = turn_time(request.data.get('ringTime',''))
        connectTime = turn_time(request.data.get('connectTime',''))
        endTime = turn_time(request.data.get('endTime',''))
        status = request.data.get('status','')
        cause = request.data.get('cause','')
        endReason = request.data.get('endReason','')
        recordUrl = request.data.get('recordUrl','')
        userData = request.data.get('userData','')
        surveyInput = request.data.get('surveyInput','')
        a = {'conversationSid': 'CV10001202105151853277824502999378', 'appSid': '17aa086feb8844798120a129ea615402', 'callType': '4', 'userNumber': '18400000000', 'agentNo': '3004', 'agentNumber': '7002000423004', 'endTime': '20210515185333', 'startTime': '20210515185327', 'answerTime': '20210515185332', 'status': '41', 'userData': {'account': '智博天下', 'Apool': '测试A池', 'docking': '测试对接网关', 'ground': '测试落地网关', 'agent_acc': '测试结算账户', 'userCli': '028account'}, 'cause': '487', 'userCli': '051880702454'}
        try:
            # call_ticket = Call_Ticket.objects.filter(conversationSid=conversationSid).first()
            # if call_ticket:
            #     return Response({"code": 403, "msg": "repetition"})
            # connect = connect_redis(11)
            # print('话单',agentNo)
            # 删除坐席忙碌状态
            seats_number = redis_query(key='%s' % conversationSid)
            redis_delete(key='%s(processing)'%seats_number)
            redis_delete(key='%s' % conversationSid)
            if request.data.get('callType') == '4' and request.data.get('status') == '44':
                pass
            else:

                callTicket_push_boss(request)
                callTicket_push_crm(request)
            # 暂不保存
            # Call_Ticket(appSid=appSid,conversationSid=conversationSid,callType=callType,userNumber=userNumber,userArea=userArea,agentNo=agentNo,agentNumber=agentNumber,userCli=userCli,agentCli=agentCli,accessNumber=accessNumber,queueId=queueId,queueTime=queueTime,startTime=startTime,answerTime=answerTime,ringTime=ringTime,connectTime=connectTime,endTime=endTime,status=status,cause=cause,endReason=endReason,recordUrl=recordUrl,userData=userData,surveyInput=surveyInput).save()
            return Response({"code": 200, "msg": "succend"})
        except Exception as e:
            Call_Ticket(conversationSid=conversationSid,error=str(e))
            return Response({"code": 500, "msg": str(e)})
    def put(self,request):
        print('PUT')
        print(request)
        return Response({"code": 200, "msg": "succend"})
    def delete(self,request):
        print('DELETE')
        print(request)
        return Response({"code": 200, "msg": "succend"})
